package com.streambolics.worseminton.data;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.streambolics.data.Person;

public class LocationInterestList extends WorsemintonObjectList<LocationInterest>
{
    private Location _Location;
    private Person   _Guest;

    public static LocationInterestList load (PersistenceManager aDatabase, Location aLocation)
    {
        return new LocationInterestList ().doLoad (aDatabase, aLocation);
    }

    public static LocationInterestList load (PersistenceManager aDatabase, Person aGuest)
    {
        return new LocationInterestList ().doLoad (aDatabase, aGuest);
    }

    @Override
    protected void bindQueryVariables (Query aQuery)
    {
        aQuery.declareParameters ("Long p_id");
    }

    @Override
    protected String buildQueryText ()
    {
        if (_Guest != null)
        {
            return standardQueryText (LocationInterest.class) + " where guestId == p_id";
        }
        else if (_Location != null)
        {
            return standardQueryText (LocationInterest.class) + " where locationId == p_id";
        }
        else
        {
            return standardQueryText (LocationInterest.class);
        }
    }

    private LocationInterestList doLoad (PersistenceManager aDatabase, Location aLocation)
    {
        _Location = aLocation;
        delayLoad (aDatabase);
        return this;
    }

    private LocationInterestList doLoad (PersistenceManager aDatabase, Person aGuest)
    {
        _Guest = aGuest;
        delayLoad (aDatabase);
        return this;
    }

    @Override
    protected Object executeQuery (Query aQuery)
    {
        if (_Guest != null)
        {
            return aQuery.execute (_Guest.getId ());
        }
        else if (_Location != null)
        {
            return aQuery.execute (_Location.getId ());
        }
        else
        {
            return super.executeQuery (aQuery);
        }
    }

}
